//
//  AssistanceUnitViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/6.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "AssistanceUnitViewController.h"
#import "AssistanceUnitTableViewCell.h"
#import "AssistanceUnitDetailViewController.h"
@interface AssistanceUnitViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, assign)int currentPage;
@property (nonatomic, strong)NSMutableArray * cellData;
@end

@implementation AssistanceUnitViewController(networking)
- (void)getAssistingList{
    NSDictionary *parameters = @{
                                 @"page":@(self.currentPage),
                                 @"size":@(8),
                                 };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"api/getAssistingList" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"msg"] isEqualToString:@"success"]) {
            if (self.currentPage == 1) {
                [self.cellData removeAllObjects];
            }
            [self.cellData addObjectsFromArray:responseObject[@"list"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(@"msg");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation AssistanceUnitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self.view addSubview:self.tableView];
    self.currentPage = 1;
    [self getAssistingList];//获取数据
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - TABBAR_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[AssistanceUnitTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AssistanceUnitTableViewCell class])];
        _tableView.rowHeight = 152/WIDTH_6_SCALE;
        __weak typeof (self)weakSelf = self;
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            weakSelf.currentPage ++;
            [self getAssistingList];
        }];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.currentPage = 1;
            [self getAssistingList];
        }];
    }return _tableView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[].mutableCopy;
    }return _cellData;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AssistanceUnitTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AssistanceUnitTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.row];
    cell.arrowBtnBlock = ^(NSString * urlStr, NSString * pageTitle) {
        AssistanceUnitDetailViewController * vc = [[AssistanceUnitDetailViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.urlStr = urlStr;
        vc.pageTitleStr = pageTitle;
        [self.navigationController pushViewController:vc animated:YES];
    };
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellData.count;
}
@end
